﻿using Microsoft.AspNetCore.Mvc;
using Microsoft.VisualBasic;
using SMARTHOTEL.hotel;
using SMARTHOTEL.RoomMoneys;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp.Application.Services;
using Volo.Abp.Domain.Repositories;

namespace SMARTHOTEL.RoomTypes
{

    /// <summary>
    /// 房间类型管理
    /// </summary>
    [ApiExplorerSettings(GroupName = "v2")]
    public class RoomTypeService : ApplicationService, IRoomTypeService
    {

        private readonly IRepository<RoomType> repository;


        public RoomTypeService(IRepository<RoomType> repository)
        {
            this.repository = repository;
        }


        /// <summary>
        /// 添加  
        /// </summary>
        /// <param name="roomTypeDto"></param>
        /// <returns></returns>
        [HttpPost("CreateAllAsync")]
        public async Task<ResultDto<bool>> CreateAllAsync(CreateUpdateRoomTypeDto roomTypeDto)
        {
            try
            {
                RoomType roomType = ObjectMapper.Map<CreateUpdateRoomTypeDto, RoomType>(roomTypeDto);

                var roomTypeList = (await repository.GetListAsync()).FirstOrDefault(x=>x.RoomTypeName==roomTypeDto.RoomTypeName);

                if (roomTypeList == null)
                {
                    await repository.InsertAsync(roomType);
                    return new ResultDto<bool> { Data = true, Code = StatusCode.Success, Message = "添加成功" };
                }
                else
                {
                    return new ResultDto<bool> { Data = false, Code = StatusCode.Faield, Message = "添加失败，房型名称已存在！" };
                }
            }
            catch (Exception ex)
            {
                return new ResultDto<bool> { Data = false, Code = StatusCode.Faield, Message = "添加失败"+ex.Message };
            }
        }

        /// <summary>
        /// 显示所有房间类型
        /// </summary>
        /// <returns></returns>
        public async Task<ResultDto<List<RoomTypeDto>>> GetSelectAsync()
        {
            var roomTypeList = await repository.GetListAsync(x=>!x.IsDeleted);

            var list = ObjectMapper.Map<List<RoomType>, List<RoomTypeDto>>(roomTypeList);

            return new ResultDto<List<RoomTypeDto>> { Data= list, Code = StatusCode.Success, Message = "查询成功" };
        }
        /// <summary>
        /// 分页
        /// </summary>
        /// <param name="roomTypeDto"></param>
        /// <returns></returns>
        public async Task<ResultDto<ApiPage<RoomTypeDto>>> GetAllAsync(string? roomTypeName,int index,int size)
        {
            var roomTypes = await repository.GetListAsync(x=>!x.IsDeleted);
            if (roomTypeName != null)
            {
                roomTypes = roomTypes.Where(x => x.RoomTypeName.Contains(roomTypeName)).ToList();
            }
            var list = ObjectMapper.Map<List<RoomType>, List<RoomTypeDto>>(roomTypes);
            ApiPage<RoomTypeDto> page = new ApiPage<RoomTypeDto>();
            page.TotalCount = list.Count;
            page.TotalPage = (int)Math.Ceiling(page.TotalCount * 1.0 / size);
            page.List=list.Skip(size*(index-1)).Take(size).ToList();
            return new ResultDto<ApiPage<RoomTypeDto>> { Data = page, Code = StatusCode.Success, Message = "查询成功" };
        }
        /// <summary>
        /// 根据id查询房间类型
        /// </summary>
        /// <returns></returns>
        [HttpGet("GetSelectAsyncid")]
        public async Task<ResultDto<RoomTypeDto>> GetSelectAsyncid(Guid roomTypeId)
        {
            var roomTypeList = await repository.FirstOrDefaultAsync(x=>x.Id==roomTypeId);

            var list = ObjectMapper.Map<RoomType, RoomTypeDto>(roomTypeList);

            return new ResultDto<RoomTypeDto> { Data = list, Code = StatusCode.Success, Message = "查询成功" };
        }
        /// <summary>
        /// 修改单个房间类型
        /// </summary>
        /// <param name="id"></param>
        /// <param name="roomTypeDto"></param>
        /// <returns></returns>
        [HttpPut("UpdateAllAsync")]
        public async Task<ResultDto<bool>> UpdateAllAsync(Guid id, CreateUpdateRoomTypeDto roomTypeDto)
        {
            try
            {
                var roomtype = await repository.GetAsync(x => x.Id == id);

                var entity = ObjectMapper.Map<CreateUpdateRoomTypeDto, RoomType>(roomTypeDto, roomtype);


                await repository.UpdateAsync(entity);

                return new ResultDto<bool> { Data = true, Code = StatusCode.Success, Message = "修改成功" };

            }
            catch (Exception ex)
            {
                return new ResultDto<bool> { Data = false, Code = StatusCode.Faield, Message = "修改失败" + ex.Message };
            }
        }
        /// <summary>
        /// 单个删除房间类型
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpDelete("DeleteOneAsync")]
        public async Task<ResultDto<bool>> DeleteOneAsync(Guid id)
        {
            try
            {
                var roomtype = await repository.GetAsync(x => x.Id == id);
                if (roomtype == null)
                {
                    return new ResultDto<bool> { Data = false, Code = StatusCode.Faield, Message = "删除失败，该数据不存在！" };
                }
                else
                {
                    await repository.DeleteAsync(roomtype);
                    return new ResultDto<bool> { Data = true, Code = StatusCode.Success, Message = "删除成功" };
                }
            }
            catch (Exception ex)
            {
                return new ResultDto<bool> { Data = false, Code = StatusCode.Faield, Message = "删除失败" + ex.Message };
            }
        }
        /// <summary>
        /// 批量删除房间类型（也可以单个删除）
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpDelete("DeleteAllAsync")]
        public async Task<ResultDto<bool>> DeleteAllAsync(string id)
        {
            try
            {
                var ids = id.Split(",");
                foreach (var item in ids)
                { 
                    var roomtype = await repository.GetAsync(x => x.Id == Guid.Parse(item));
                    if (roomtype != null)
                    {
                        roomtype.IsDeleted = true; 
                        await repository.UpdateAsync(roomtype);
                    }
                }
                return new ResultDto<bool> { Data = false, Code = StatusCode.Faield, Message = "删除成功"  };

            }
            catch (Exception ex)
            {
                return new ResultDto<bool> { Data = false, Code = StatusCode.Faield, Message = "删除失败"+ex.Message };
            }
        }
    }
}
